package com.eims.sls_app.fragment;

import com.eims.main.livescores.fragment.MainLqBifenFragment;
import com.eims.main.livescores.fragment.MainZqBifenFragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.TextView;
import bls.juejin.cn.R;

public class LiveScoresFragment extends Fragment implements OnClickListener {
	public final static String TAG = "LiveScoresFragment";
	private TextView tvBifenZq, tvBifenLq;
	private int tvId;
	private int position;
	private Bundle saveState;
	private TextView tvFresh;

	public static Fragment newInstance(int position) {
		Fragment liveScoresFragment = new LiveScoresFragment();
		Bundle bundle = new Bundle();
		// Log.i("Hall", "Betfair"+position);
		bundle.putInt("position", position);
		// position = position;
		liveScoresFragment.setArguments(bundle);
		return liveScoresFragment;
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		Bundle bundle = getArguments();
		// MyToast.getToast(getActivity(), TAG+bundle);
		if (bundle == null) {
			position = 0;
		} else {
			position = bundle.getInt("position");
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		Context contextThemeWrapper = new ContextThemeWrapper(getActivity(), R.style.StyledIndicators);
		LayoutInflater localInflater = inflater.cloneInContext(contextThemeWrapper);
//		inflater.inflate(R.layout.fragment_zq_bifen, container, false);
		View v = inflater.inflate(R.layout.main_bifen_activity, container, false);

		tvBifenZq = (TextView) v.findViewById(R.id.tv_bifen_zq);

		tvBifenLq = (TextView) v.findViewById(R.id.tv_bifen_lq);
		tvFresh=(TextView) v.findViewById(R.id.tv_fresh);
		tvBifenZq.setOnClickListener(this);
		tvBifenLq.setOnClickListener(this);
		tvFresh.setOnClickListener(this);
		
		changeFragment(1,false);
		return v;
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.tv_bifen_zq:
			if (tvId != R.id.tv_bifen_zq) {
				changeFragment(1,false);
			}

			break;
		case R.id.tv_bifen_lq:
			if (tvId != R.id.tv_bifen_lq) {
				changeFragment(2,false);
			}
			break;
		case R.id.tv_fresh:
			if (tvId == R.id.tv_bifen_lq) {
				changeFragment(2,true);
			}else {
				changeFragment(1,true);
			}
			break;
		default:
			break;
		}
	}

	public void changeFragment(int i,boolean isfresh) {
		if (i == 1) {
			tvId = R.id.tv_bifen_zq;
			tvBifenZq.setBackgroundResource(R.drawable.top_choice_bg);
			tvBifenLq.setBackgroundResource(R.drawable.top_choice_lr_bg);

			tvBifenZq.setTextColor(getResources().getColor(R.color.red));
			tvBifenLq.setTextColor(getResources().getColor(R.color.white));

			FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
			transaction.replace(R.id.fragment_container,  MainZqBifenFragment.newInstance(isfresh), "zq");

			transaction.commitAllowingStateLoss();

		} else if (i == 2) {
			tvId = R.id.tv_bifen_lq;
			tvBifenLq.setBackgroundResource(R.drawable.top_choice_bg);
			tvBifenZq.setBackgroundResource(R.drawable.top_choice_lr_bg);

			tvBifenLq.setTextColor(getResources().getColor(R.color.red));
			tvBifenZq.setTextColor(getResources().getColor(R.color.white));
			FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
			transaction.replace(R.id.fragment_container, MainLqBifenFragment.newInstance(isfresh), "lq");

			transaction.commitAllowingStateLoss();

		}
	}

}
